/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import entities.Material;
import entities.Supplier;
import entities.SupplierXpyme;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author PabloSebastian
 */
@Stateless
@Remote(SupplierXmaterialFacadeRemote.class)
public class SupplierXmaterialFacade extends AbstractFacade<Material> implements SupplierXmaterialFacadeRemote {
    @PersistenceContext(unitName = "Javeriana.EJB.EstimatorPU")
    private EntityManager em;
    @EJB
    private SupplierFacadeRemote suplierFacade;
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SupplierXmaterialFacade() {
        super(Material.class);
    }

    @Override
    public void saveDefaultSupplier(int idSupplier , int pymeIdentification) {
         
        boolean editado = false;
        Supplier supplier = suplierFacade.find(idSupplier);
        Material material = supplier.getMaterial();
        material = this.find(material.getTypematerial());
        List<SupplierXpyme> supplierXmaterial = material.getSupplierXpymeList();
        for (int i = 0; i < supplierXmaterial.size(); i++ ) 
        {
            SupplierXpyme supplierXpyme = supplierXmaterial.get(i);
            if (supplierXpyme.getSupplierXpymePK().getPymeidentification() == pymeIdentification )
            {
                material.getSupplierXpymeList().set(i,(new SupplierXpyme(idSupplier, pymeIdentification)));
                i = supplierXmaterial.size();
                editado = true;
            }
        }
        if (editado)
        {
            this.edit(material);
        }
        else
        {
            material.getSupplierXpymeList().add(new SupplierXpyme(idSupplier, pymeIdentification));
            this.edit(material);
        }
        
    }
    
}
